package com.gitee.dictionary.config;

import com.gitee.dictionary.init.InitDictionaryEnums;
import com.gitee.dictionary.base.DictionaryEnumDao;
import com.gitee.dictionary.base.impl.DefaultDictionaryEnumDaoImpl;
import com.gitee.dictionary.cache.DictionaryEnumCache;
import com.gitee.dictionary.properties.ConfigProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@Configuration
@EnableConfigurationProperties({ConfigProperties.class})
public class DictionaryEnumConfig {
    
    private final ConfigProperties configProperties;
    
    public DictionaryEnumConfig(ConfigProperties configProperties) {
        this.configProperties = configProperties;
    }
    
    @Bean
    @ConditionalOnMissingBean(InitDictionaryEnums.class)
    public InitDictionaryEnums initDictionaryEnums(DictionaryEnumCache dictionaryEnumCache) {
        return new InitDictionaryEnums(dictionaryEnumCache);
    }
    
    @Bean
    @ConditionalOnMissingBean(DictionaryEnumCache.class)
    public DictionaryEnumCache dictionaryEnumIntegerCache(DictionaryEnumDao dictionaryEnumDao) {
        return new DictionaryEnumCache(this.configProperties, dictionaryEnumDao);
    }
    
    @Bean
    @ConditionalOnMissingBean(DictionaryEnumDao.class)
    public DictionaryEnumDao dictionaryEnumDao() {
        return new DefaultDictionaryEnumDaoImpl(this.configProperties);
    }
}
